package com.snow.dayou.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * All rights Reserved, Designed By www.cusc.com
 *
 * @author yanke
 * @Title: ApplicationHelper.java
 * @Package com.faw.api.portal.common.util
 * @Description:jackjson一些转换方法
 * @Email yanke@cu-sc.com
 * @version: v1.0.0
 * @date: 2019年9月3日 上午11:41:58
 * @Copyright: 2019 www.cusc.com Inc. All rights reserved. <br/>
 * 注意：本内容仅限于一汽集团内部传阅，禁止外泄以及用于其他的商业目<br/>
 * @since JDK 1.8
 */
@Component
public class ApplicationHelper implements ApplicationContextAware {


    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(ApplicationHelper.applicationContext == null){
            ApplicationHelper.applicationContext  = applicationContext;
        }
        //print("---------------simple.plugin.spring.SpringUtil------------------------------------------------------");
        //print("========ApplicationContext配置成功,在普通类可以通过调用ApplicationHelper.getAppContext()获取applicationContext对象,"/+ "applicationContext= " + ApplicationHelper.applicationContext + "========");
    }


    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);

    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}
